package com.evil.common.application.enums.permission;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 控件可见状态
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ControlVisibleEnum {

    /**
     * 控件可见状态 (0:不可见, 1:仅可见, 2:可编辑)
     **/
    INVISIBLE(0, "隐藏"),
    VISIBLE(1, "显示"),
    EDIT(2, "可编辑"),
    ;
    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ControlVisibleEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ControlVisibleEnum isExistById(Integer id) {
        return ControlVisibleEnum.findById(id).orElseThrow(()
                -> new BusinessException(RCodeEnum.ERROR_PARAMETER, "控件可见状态参数错误"));
    }

    public static int getSpecialControlVisible(boolean visible) {
        return visible ? VISIBLE.id : INVISIBLE.id;
    }
}
